;
(function() {

	var _bind_state_arr;
	var _init_bind_state_arr = function() {
		_bind_state_arr = [];
		Object.keys(BusUserCon.STATE).forEach(function(key) {
			_bind_state_arr[BusUserCon.STATE[key]] = key;
		});
	};

	var skill = {
		save: function() {
			delete this.bus;
			delete this.user;
			db.update(db_table_name, this._id, this);
		},
		destroy: function() {
			db.remove(db_table_name, this._id);
		},
		_nextFlowStatus: function() {
			var state = +this.state;
			console.log(state);
			if (state || state == 0) {
				if (!_bind_state_arr) {
					_init_bind_state_arr();
				}
				//如果有下一个状态值，进入
				if (_bind_state_arr[state + 1]) {
					state += 1;
				}
			} else {
				throwCusError("未知的兑现状态")
			}
			console.log(state);
			this.state = state;
			this.save();
			return state;
		},
		_toLog: function(msg) {
			return {
				msg: msg,
				data: JSON.stringify({
					state: this.state,
					money_used: this.money_used,
					money_to_cash: this.money_to_cash,
					money: this.money,
					money_apply: this.money_apply,
					bina_acc_to_money: this.bina_acc_to_money
				}),
				time: +new Date
			}
		},
		getLogs: function(filter_config, log_num) {
			filter_config || (filter_config = {});
			log_num || (log_num = Infinity); //过滤条数
			var _filter_msg = String(filter_config.msg || "");
			var _filter_time = +filter_config.time;
			var logs = this.logs || (this.logs = []);
			var res;
			var _filter_handles = [];
			var _is_stop; //可以被filter_handle决定中断循环的参数
			if (_filter_msg) {
				_filter_handles.push(function(log) {
					return log.msg.indexOf(_filter_msg) !== -1;
				})
			}
			if (_filter_time) {
				_filter_handles.push(function(log) {
					//日志信息都是按照时间在排序，所以一旦到时间线，就可以中断所有循环
					if (log.time < _filter_time) {
						return true
					} else {
						_is_stop = true;
					}
				})
			}
			if (_filter_handles.length) {
				res = [];
				logs.some(function(log) {
					if (_filter_handles.every(function(_filter_handle) {
							return _filter_handle(log)
						})) {
						res.push(log)
						if (res.length >= log_num) {
							return true;
						}
						return _is_stop;
					}
				});
			} else {
				res = logs;
			}
			return res;
		},
		addLogs: function(msg) {
			var logs = this.logs || (this.logs = []);
			logs.unshift(this._toLog(msg))
			this.save();
		},
		//改变最近一条日志信息
		changCurrentLogs: function(msg) {
			var logs = this.logs || (this.logs = []);
			var log = logs[0];
			if (log) {
				var new_log = this._toLog(msg);
				log.msg = new_log.msg;
				log.data = new_log.data;
			};
			this.save();
		},
		//非安全，将份额使用掉
		_useMoney: function(money_num, msg) {
			//格式化
			money_num = parseFloat(money_num) || 0;
			if (money_num < 0) {
				throwCusError("使用的金额不可为负数")
			}
			//上限校验
			if (money_num > this.money) {
				throwCusError("可用金额不足")
			}
			console.log(this.money, this.money_used);
			this.money -= money_num;
			this.money_used += money_num;
			console.log(this.money, this.money_used);
			this.save();

			this.addLogs((msg || "使用金额") + "：" + money_num);
		},
		//非安全，退还钱
		_unUseMoney: function(money_num, msg) {
			//格式化
			money_num = parseFloat(money_num) || 0;
			if (money_num < 0) {
				throwCusError("退还金额有误")
			}
			console.log(this.money, this.money_used);
			this.money += money_num;
			this.money_used -= money_num;
			console.log(this.money, this.money_used);
			this.save();

			this.addLogs((msg || "使用金额") + "：" + money_num);
		},
		//非安全，申请提现
		_applyMoney: function(money_num) {
			//格式化
			money_num = parseFloat(money_num) || 0;
			if (money_num < 0) {
				throwCusError("使用的金额不可为负数")
			}
			if (money_num === 0) {
				throwCusError("提现金额不可为空");
			}
			//上限校验
			if (money_num > this.money) {
				throwCusError("可用额度不足")
			}
			var bus_info = BusCon.get(this.bus_id).info;
			console.log(money_num, bus_info.data_get_cash_max_each_time)
			if (bus_info.data_get_cash_max_each_time && money_num > bus_info.data_get_cash_max_each_time) {
				throwCusError("提现金额超出每次提取额度上限");
			}
			if (bus_info.data_get_cash_times_each_month) {
				//月初
				var _month_start_time_line = +new Date((new Date).format("yyyy-MM-dd") + " 0:0:0:0");
				var _apply_logs_length = this.getLogs({
					//月初
					msg: "申请提现：",
					time: _month_start_time_line
				}).length - this.getLogs({
					msg: "取消提现申请：",
					time: _month_start_time_line
				}).length;
				if (_apply_logs_length >= bus_info.data_get_cash_times_each_month) {
					throwCusError("本月提现次数已经达到上线");
				}
			}

			console.log(this.money, this.money_apply);
			this.money -= money_num;
			this.money_apply += money_num;
			console.log(this.money, this.money_apply);
			this.save();

			this.addLogs("申请提现：" + money_num);
		},
		//非安全，取消申请提现
		_cancelApplyMoney: function() {
			this.money += this.money_apply || 0;
			this.money_apply = 0;
			this.save();

			this.addLogs("取消提现申请：" + money_num);
		},
		_confirmApplyMoney: function() {
			var money_num = this.money_apply;
			this.money_to_cash += money_num;
			this.money_apply = 0;
			this.save();

			this.addLogs("完成提现申请：" + money_num);
		},
		//非安全，添加可用额度
		_addMoney: function(money_num, log) {
			//格式化
			money_num = parseFloat(money_num) || 0;
			if (money_num < 0) {
				throwCusError("添加的金额不可为负数")
			}
			console.log(this.money, this.money_used);
			this.money += money_num;
			console.log(this.money, this.money_used);
			this.save();

			this.addLogs(log || "转入金额：" + money_num);
			return money_num;
		},
		//非安全，添加可用二进制额度
		_addBinaMoney: function(money_num) {
			//格式化
			money_num = this._addMoney(money_num);
			this.bina_acc_to_money += money_num;
			this.save();

			this.changCurrentLogs("（商品二进制）转入金额" + money_num);
		}
	};

	module.exports = skill;

	var db = require("../db/index");
	var config = require("../_include/config");
	var db_table_name = config.dbname.busUser;
	var db_id_prefix = config.dbIdPrefix.busUser;
	var UserCon = require("../user/_con");
	var BusCon = require("../bus/_con");
	var AchCon = require("../ach/_con");
	var BusUserCon = require("./_con");

}());